#include "Observer.h"
#include "Subject.h"

Observer::Observer()
{
	_stat = '\0';
}

Observer::~Observer()
{
	
}

ContentObserver::ContentObserver(Subject* sub)
{
	_sub = sub;
	_sub->Attach(this);

	_img = nullptr;
}

ContentObserver::~ContentObserver()
{
	_sub->Detach(this);

	if (_img) cvReleaseImage(&_img);
}

Subject* ContentObserver::GetSubject()
{
	return _sub;
}

void ContentObserver::Show()
{
	cvNamedWindow("image");

	if (_img) cvReleaseImage(&_img);
	_img = cvLoadImage(_stat.c_str());

	cvShowImage("image", _img);
	cvWaitKey();
}

void ContentObserver::Update(Subject* sub)
{
	_stat = sub->GetState();

	Show();
}

SizeObserver::SizeObserver(Subject* sub)
{
	_sub = sub;
	_sub->Attach(this);

	_dim[0] = _dim[1] = 0;
}

SizeObserver::~SizeObserver()
{
	_sub->Detach(this);
}

Subject* SizeObserver::GetSubject()
{
	return _sub;
}

void SizeObserver::Show()
{
	IplImage* img = cvLoadImage(_stat.c_str());
	_dim[0] = img->width;
	_dim[1] = img->height;
	cvReleaseImage(&img);

	cout << "The width is " << _dim[0] << endl;
	cout << "The height is " << _dim[1] << endl;
}

void SizeObserver::Update(Subject* sub)
{
	_stat = sub->GetState();

	Show();
}
